package com.yanggeng.demo.beginnerguidedemo.activity.customview;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.activity.BaseActivity;
import com.yanggeng.demo.beginnerguidedemo.bean.ConsumeMonthBean;
import com.yanggeng.demo.beginnerguidedemo.bean.MileageStatisticsBean;
import com.yanggeng.demo.beginnerguidedemo.utils.DateUtils;
import com.yanggeng.demo.beginnerguidedemo.utils.ToastUtil;
import com.yanggeng.demo.beginnerguidedemo.widget.AutoScrollTextView;
import com.yanggeng.demo.beginnerguidedemo.widget.ConsumeLineChartView;
import com.yanggeng.demo.beginnerguidedemo.widget.GradientProgressView;
import com.yanggeng.demo.beginnerguidedemo.widget.OilConsumeLineChartView;
import com.yanggeng.demo.beginnerguidedemo.widget.PayoffLineChart;
import com.yanggeng.demo.beginnerguidedemo.widget.ScrollLineChartView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by yanggeng on 2017/8/2.
 */

public class TestActivity extends BaseActivity {

    private AutoScrollTextView textView;
    private Context context;
    private GradientProgressView progressView;
    private TextView tv_test;
    private ConsumeLineChartView lineChartView;
    private PayoffLineChart payoffLineChart;
    private ScrollView svTest;
    private OilConsumeLineChartView oilConsumeLineChartView;
    private ScrollLineChartView scrollLineChartView;

    private List<Float> yearWeekValues = new ArrayList<>(); //七日年化
    private List<Float> yearMonthValues = new ArrayList<>(); //一个月年化
    private List<Float> year3MonthValues = new ArrayList<>(); //3个月年化

    private List<Float> earnWeekValues = new ArrayList<>(); //七日收益
    private List<Float> earnMonthValues = new ArrayList<>(); //一个月收益
    private List<Float> earn3MonthValues = new ArrayList<>(); //三个月收益

    private int currentTypeMode = PayoffLineChart.MODE_TYPE_YEAR_FLOAT;
    private int currentDateMode = PayoffLineChart.MODE_WEEK;

    private float oldX = -1, oldY = -1;

    private List<ConsumeMonthBean> oilData = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        setTitle(true, "Test");
        context = this;

        // 垂直轮播文本框
        textView = (AutoScrollTextView) findViewById(R.id.tv_scroll);
        tv_test = (TextView) findViewById(R.id.tv_test);
        svTest = (ScrollView) findViewById(R.id.sv_test);
        textView.setText(16, 8, Color.WHITE);
        textView.setAnimDuration(300);
        ArrayList<String> textList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            textList.add("这是第" + i + "条滚动消息啦啦啦啦啦啦啦啦啦啦啦啦");
        }
        textView.setTextList(textList);
        textView.setScrollDuration(3000);
        textView.setOnItemClickListener(new AutoScrollTextView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ToastUtil.centerToast(context, "点击了第" + position + "条消息");
            }
        });

        // 渐变色进度条
        progressView = (GradientProgressView) findViewById(R.id.progress);
        progressView.setCurrentPosition(85);
        tv_test.setTextColor(progressView.getColor());
        final Random random = new Random();
        findViewById(R.id.btn_do).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressView.setCurrentPosition(random.nextInt(100));
            }
        });

        findViewById(R.id.btn_week).setOnClickListener(this);
        findViewById(R.id.btn_month).setOnClickListener(this);
        findViewById(R.id.btn_3_month).setOnClickListener(this);
        findViewById(R.id.btn_year_float).setOnClickListener(this);
        findViewById(R.id.btn_earn).setOnClickListener(this);

        findViewById(R.id.btn_get_date).setOnClickListener(this);

        // 折线图
        lineChartView = (ConsumeLineChartView) findViewById(R.id.chart);


        payoffLineChart = (PayoffLineChart) findViewById(R.id.payoff_line);
        payoffLineChart.setMode(currentTypeMode, currentDateMode);
        payoffLineChart.setData(yearWeekValues, DateUtils.getLastWeekDate());
        lineChartView.setInfo(new String[]{"2017-5", "2017-6", "2017-7", "2017-8", "2017-9"},
                new float[]{10, 500, 80, 2500.6f, 0}, false); //, "2017-6", "2017-7", "2017-8", "2017-9"  , 500, 80, 2500.6f, 0

        payoffLineChart.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    if (oldX != -1 && oldY != -1) {
                        float scrollX = event.getX() - oldX;
                        float scrollY = event.getY() - oldY;
                        if (Math.abs(scrollX) - Math.abs(scrollY) > 20) {
                            svTest.requestDisallowInterceptTouchEvent(true);
                        }
                    }
                    oldX = event.getX();
                    oldY = event.getY();
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    svTest.requestDisallowInterceptTouchEvent(false);
                }
//                else {
//                    svTest.requestDisallowInterceptTouchEvent(true);
//                }
                return false;
            }
        });

        oilConsumeLineChartView = (OilConsumeLineChartView) findViewById(R.id.line_chart_oil_consume);
        oilData.add(new ConsumeMonthBean("2017-08-03", "5.0"));
        oilData.add(new ConsumeMonthBean("2017-09-29", "16.0"));
        oilData.add(new ConsumeMonthBean("2017-10-04", "12.0"));
        oilData.add(new ConsumeMonthBean("2017-10-12", "0"));
        oilData.add(new ConsumeMonthBean("2017-10-31", "5.0"));
        oilConsumeLineChartView.setData(oilData);

        scrollLineChartView = (ScrollLineChartView) findViewById(R.id.scroll_line_chart);
        List<MileageStatisticsBean> data = new ArrayList<>();
        data.add(new MileageStatisticsBean("2017.12.04", 20.1f));
        data.add(new MileageStatisticsBean("2017.12.05", 8f));
        data.add(new MileageStatisticsBean("2017.12.06", 0f));
        data.add(new MileageStatisticsBean("2017.12.07", 10.1f));
        data.add(new MileageStatisticsBean("2017.12.08", 18.6f));
        data.add(new MileageStatisticsBean("2017.12.09", 34.5f));
        data.add(new MileageStatisticsBean("2017.12.10", 50.1f));
        data.add(new MileageStatisticsBean("2017.12.11", 10.1f));
        data.add(new MileageStatisticsBean("2017.12.12", 30.1f));
        data.add(new MileageStatisticsBean("2017.12.13", 20.1f));
        scrollLineChartView.setData(data);
    }

    private List<Float> initEarnValue(int count) {
        List<Float> temp = new ArrayList<>();
        for (int i = 0; i < count; i ++) {
            temp.add((float) (Math.random() * 4));
        }
        return temp;
    }

    private List<Float> initYearValue(int count) {
        List<Float> temp = new ArrayList<>();

        for (int i = 0; i < count; i ++) {
            temp.add((float) (Math.random() * 5));
        }
        return temp;
    }

    @Override
    protected void onResume() {
        super.onResume();
        textView.startScroll();
    }

    @Override
    protected void onStop() {
        super.onStop();
        textView.stopScroll();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_week:
                currentDateMode = PayoffLineChart.MODE_WEEK;
                updateUI();
                break;
            case R.id.btn_month:
                currentDateMode = PayoffLineChart.MODE_MONTH;
                updateUI();
                break;
            case R.id.btn_3_month:
                currentDateMode = PayoffLineChart.MODE_3_MONTH;
                updateUI();
                break;
            case R.id.btn_year_float:
                currentTypeMode = PayoffLineChart.MODE_TYPE_YEAR_FLOAT;
                updateUI();
                break;
            case R.id.btn_earn:
                currentTypeMode = PayoffLineChart.MODE_TYPE_EARN;
                updateUI();
                break;
            case R.id.btn_get_date:
                earnWeekValues = initEarnValue(7);
                earnMonthValues = initEarnValue(30);
                earn3MonthValues = initEarnValue(90);
                yearWeekValues = initYearValue(7);
                yearMonthValues = initYearValue(30);
                year3MonthValues = initYearValue(90);
                updateUI();
                break;
        }
    }

    private void updateUI() {
        payoffLineChart.setMode(currentTypeMode, currentDateMode);
        switch (currentDateMode) {
            case PayoffLineChart.MODE_WEEK:
                switch (currentTypeMode) {
                    case PayoffLineChart.MODE_TYPE_YEAR_FLOAT:
                        payoffLineChart.setData(yearWeekValues, DateUtils.getLastWeekDate());
                        break;
                    case PayoffLineChart.MODE_TYPE_EARN:
                        payoffLineChart.setData(earnWeekValues, DateUtils.getLastWeekDate());
                        break;
                }
                break;
            case PayoffLineChart.MODE_MONTH:
                switch (currentTypeMode) {
                    case PayoffLineChart.MODE_TYPE_YEAR_FLOAT:
                        payoffLineChart.setData(yearMonthValues, DateUtils.getLastMonthDate());
                        break;
                    case PayoffLineChart.MODE_TYPE_EARN:
                        payoffLineChart.setData(earnMonthValues, DateUtils.getLastMonthDate());
                        break;
                }
                break;
            case PayoffLineChart.MODE_3_MONTH:
                switch (currentTypeMode) {
                    case PayoffLineChart.MODE_TYPE_YEAR_FLOAT:
                        payoffLineChart.setData(year3MonthValues, DateUtils.getLast3MonthDate());
                        break;
                    case PayoffLineChart.MODE_TYPE_EARN:
                        payoffLineChart.setData(earn3MonthValues, DateUtils.getLast3MonthDate());
                        break;
                }
                break;
        }

    }


}
